package com.swsc.solarverse.service.analysis.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swsc.solarverse.dao.entity.analysis.StringEfficiencyDayDO;
import com.swsc.solarverse.dao.mapper.analysis.StringEfficiencyDayMapper;
import com.swsc.solarverse.service.analysis.IStringEfficiencyDayService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-08-30
 */
@Service
public class StringEfficiencyDayServiceImpl extends ServiceImpl<StringEfficiencyDayMapper, StringEfficiencyDayDO> implements IStringEfficiencyDayService {

    @Override
    public List<StringEfficiencyDayDO> sumMonth(Integer year, Integer month) {
        return baseMapper.sumMonth(year, month);
    }

    @Override
    public List<StringEfficiencyDayDO> sumYear(Integer year) {
        return baseMapper.sumYear(year);
    }

    @Override
    public List<StringEfficiencyDayDO> sumTotal() {
        QueryWrapper<StringEfficiencyDayDO> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select( " `tenant_no`, `organ_no`, `device_pid`, `device_id`, `device_name`, SUM( `efficiency` ) efficiency ")
                .groupBy(" `tenant_no`, `organ_no`, `device_pid`, `device_id`, `device_name` ");
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        // (year < 2023 and month <= 12) or (year = 2023 and month < 3)
        queryWrapper.or(wr -> wr.lt("`year`", year).le("`month`", 12))
                .or(wr -> wr.eq("`year`", year).le("`month`", month));
        return baseMapper.selectList(queryWrapper);
        // return baseMapper.sumTotal();
    }
}
